import * as mars3d from 'mars3d'
import { mapConfig } from './map.config'
// import editorware from '../tool/utils/editorMiddleware'
// import store from '@/store'
import request from '../../utils/request'
class marsMap {
    constructor() {
        // this.mars3d = mars3d
        // this.Cesium = mars3d.Cesium
        // this.map = ''
        this.vm = ''
        // this.graphicLayer = ''
        // 添加的点线面图层
        this.coverageMap = new Map()
        // 当前套创建的对象的基类
        this.graphicLayer = ''
        // 等高线预处理
        // this.contourLine = ''
        this.setMapConfig = {}
        // 测距
        this.measure = ''
    }

    init(vue) {
        this.vm = vue
        if (!mars3d.Util.webglreport()) {
            mars3d.Util.webglerror()
            return
        }
        loadingFunc().then(res => {
            this.initMars3d()
        })
    }

    initMars3d() {
        //合并属性参数，可覆盖config.json中的对应配置
        // if(this.setMapConfig){
        // let mapOptions = mars3d.Util.merge(mapConfig, this.setMapConfig)
        // }

        // 创建三维地球场景
        var map = new mars3d.Map(`mars3d-container${this.vm.mapKey}`, mapConfig)
        window.map = map
        window.mars3d = mars3d
        window.Cesium = mars3d.Cesium
        window.contourLine = undefined
        // this.map = map
        // console.log(map)
        map.openFlyAnimation({
            // duration1:4,
            // easingFunction1: Cesium.EasingFunction.QUINTIC_IN_OUT,
            callback: () => {
                // 动画播放完成后回调

                this.graphicLayer = new mars3d.layer.GraphicLayer({
                    hasEdit: false,
                    isAutoEditing: false // 绘制完成后是否自动激活编辑
                    // isRestorePositions: true
                })
                map.addLayer(this.graphicLayer)
                // 测距
                this.measure = new mars3d.thing.Measure({
                    label: {
                        color: '#ffffff',
                        font_family: '楷体',
                        font_size: 20,
                        background: false
                    }
                })
                map.addThing(this.measure)

                contourLine = new mars3d.thing.ContourLine({
                    contourShow: true,
                    spacing: 50,
                    width: 1,
                    color: Cesium.Color.RED,
                    shadingType: 'none',
                    showElseArea: true,
                    //地表渲染的配色方案
                    colorScheme: {
                        elevation: {
                            step: [0.0, 0.045, 0.1, 0.15, 0.37, 0.54, 1.0],
                            color: ['#000000', '#2747E0', '#D33B7D', '#D33038', '#FF9742', '#FF9742', '#ffd700']
                        },
                        slope: {
                            step: [0.0, 0.29, 0.5, Math.sqrt(2) / 2, 0.87, 0.91, 1.0],
                            color: ['#000000', '#2747E0', '#D33B7D', '#D33038', '#FF9742', '#FF9742', '#ffd700']
                        },
                        aspect: {
                            step: [0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0],
                            color: ['#000000', '#2747E0', '#D33B7D', '#D33038', '#FF9742', '#FF9742', '#ffd700']
                        }
                    }
                })
                map.addThing(contourLine)
                /**
                 * 增加放大缩小按钮
                 */
                const zoom = new mars3d.control.Zoom({
                    insertIndex: 1, // 插入的位置顺序
                    zoomInClass: 'fa fa-plus',
                    zoomOutClass: 'fa fa-minus'
                })
                map.addControl(zoom)
            }
        })

        // /13/12698/2527.png
        // let tileLayer111 = new mars3d.layer.XyzLayer({
        //     url: 'http://192.168.0.102/{z}/{x}/{y}.png',
        //     minimumLevel: 13,
        //     maximumLevel: 20,
        //     rectangle: { xmin: 73.16895, xmax: 134.86816, ymin: 12.2023, ymax: 54.11485 },
        //     opacity: 1,
        //     CRS: 'EPSG:3857'
        //     // flyTo: true
        // })
        // map.addLayer(tileLayer111)

        // request.download('http://59.110.54.18/file/1.png').then(res => {
        //     console.log(res)
        // })
        // const xhr = new XMLHttpRequest()
        // xhr.responseType = 'blob'
        // xhr.onload = function() {
        //     console.log(xhr)
        //     return
        //     let blobUrl = ''
        //     blobUrl = URL.createObjectURL(xhr.response)
        //     if (blobUrl) {
        //         // array[index].url = blobUrl
        //         let layer = new mars3d.layer.ImageLayer({
        //             url: blobUrl,
        //             rectangle: { xmin: 73.16895, xmax: 134.86816, ymin: 12.2023, ymax: 54.11485 },
        //             alpha: 1
        //         })
        //         map.addLayer(layer)
        //     }
        // }
        // xhr.open('GET', 'http://59.110.54.18/file/1.png')
        // xhr.send()
    }
    flyTo(lngLat) {
        lngLat.push(1000)
        map.flyToPoint(lngLat, { radius: 1000 })
    }
}
export const initMap = new marsMap()
// console.log(initMap)
export const Mars3d = mars3d
function loadingFunc() {
    const js = [
        './js/jquery-2.1.4.min.js',
        './js/graphicManager.js',
        './js/kml-geojson.js',
        './js/haoutil.js'
        // './js/Kml2JsonLayer.js'
        // 'http://mars3d.cn/lib/mars3d/mars3d.js'
        //  'http://mars3d.cn/example/js/common.js'
    ]
    // const css = ['./css/style.css', 'https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css']
    const css = ['./css/style.css', './css/icons/css/font-awesome.min.css']
    return new Promise((reslove, reject) => {
        // 加载css
        const link = css.map(v => {
            const css = document.createElement('link')
            css.rel = 'stylesheet'
            css.async = false
            css.href = v
            document.head.appendChild(css)
        })
        // 加载js
        const script = js.map(v => {
            const js = document.createElement('script')
            js.type = 'text/javascript'
            js.async = false
            js.src = v
            document.body.appendChild(js)
            return js
        })
        const end = script.pop()
        end.onload = reslove
    })
}
